import util.StringUtil;

import java.util.ArrayList;

/**
 * Created by gamy on 2017/7/15.
 */

public class ConvertSQLToResultMap {


    public String convert(ArrayList<TableProperty> list) {
        String s = "";
        for (TableProperty tp : list) {
            if (ignoreTable(tp.tableName)) {
                continue;
            }
            s += handleTableNameBegin(tp.tableName) + "\n";
            for (TableProperty.TableColumn column : tp.columns) {
                s += handleColumn(column) + "\n";
                ;
            }
            s += handleTableNameEnd(tp.tableName) + "\n\n";
            ;
        }
        return s;
    }


    private String handleTableNameBegin(String str) {
        String s = "<resultMap type=\"com.st.dao." + str.toLowerCase() + "." + str;
        s += "\" id =\"";
        s += str;
        s += "ResultMap\">";
        return s;
    }

    private String handleColumn(TableProperty.TableColumn column) {
        if (column.isPK) {
            //如果是主键
            return "    <id property=\"" + changeIntoProperty(column.column) + "\" column=\"" + column.column + "\"/>";
        } else {
            return "    <result property=\"" + changeIntoProperty(column.column) + "\" column=\"" + column.column + "\"/>";
        }
    }

    private String changeIntoProperty(String str) {
        return StringUtil.underline2Camel(str, true);
    }

    private String handleTableNameEnd(String str) {
        return "</resultMap>\n";
    }

    private boolean ignoreTable(String str) {
        if (str == null || str == "" || str.contains("Index")) {
            return true;
        }
        ;
        return false;
    }

}
